﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CineProwl.Domain.MovieDb
{
    public class Movie : BaseMovieDb
    {
        public int id;

        public string language;

        public string original_name;

        public string name;

        public string alternative_name;

        public string movie_type;

        public string imdb_id;

        public string url;

        public int? votes;

        public double? rating;

        public string status;

        public string tagline;

        public string certification;

        public string overview;

        public string[] keywords; //Details -> More

        public string released;

        public int? runtime;

        public int? budget; //Details -> More

        public int? revenue; //Details -> More

        public string homepage;

        public string trailer;

        public Genre[] genres;

        public Studio[] studios; //Details -> More

        public Language[] countries;

        public Image[] posters;

        public Image[] backdrops;

        public Cast[] cast;

        public Language[] languages_spoken;

        public int movieDbId;

        public CineProwl.Domain.Movie ToMovie()
        {
            var movie = new CineProwl.Domain.Movie();

            movie.AlternativeName = alternative_name ?? "";
            movie.Backdrops = backdrops != null ? GetImageList(backdrops) : new List<ImageFile>();
            movie.Budget = budget ?? 0;
            movie.Countries = countries != null ? countries.Select(c => c.name).ToList() : new List<string>();
            movie.Genres = genres != null ? genres.Select(g => g.name).ToList() : new List<string>();
            movie.ImdbId = imdb_id;
            movie.Keywords = keywords != null ? keywords.ToList() : new List<string>();
            movie.MovieDbUrl = url;
            movie.MovieDdId = id;
            movie.Mpaa = certification;
            movie.Name = name;
            movie.OriginalName = original_name;
            movie.Overview = overview;
            movie.Cast = cast != null ? GetCastList() : new List<CineProwl.Domain.Role>();
            movie.Posters = posters != null ? GetImageList(posters) : new List<ImageFile>();
            movie.Rating = rating ?? 0;
            movie.Revenue = revenue ?? 0;
            movie.Runtime = runtime ?? 0;
            movie.Studios = studios != null ? studios.Select(s => s.name).ToList() : new List<string>();
            movie.TagLine = tagline;
            movie.Trailer = trailer;
            
            DateTime date;
            if (DateTime.TryParse(released, out date))
            {
                movie.ReleaseDate = date;
            }
            return movie;
        }

        private List<CineProwl.Domain.Role> GetCastList()
        {
            var roles = (from c in cast
                         select new CineProwl.Domain.Role()
                                {
                                    Character = c.character,
                                    Department = c.department,
                                    Job = c.job,
                                    Profile = c.profile
                                }).ToList();
            return roles;
        }
    }
}
